import { APP_CACHE_CONFIG } from "@/config";

const getKeyFinal = (key: string) => `${APP_CACHE_CONFIG.namespace}/${key}`;

/** 携带命名空间的localStorage */
export const localStorageWithNamespace: Storage = {
  setItem: (key: string, value: string) => {
    return localStorage.setItem(getKeyFinal(key), value);
  },
  getItem: (key: string) => {
    return localStorage.getItem(getKeyFinal(key));
  },
  removeItem: (key: string) => {
    return localStorage.removeItem(getKeyFinal(key));
  },
  clear: () => {
    return localStorage.clear();
  },
  key: (index: number) => {
    return localStorage.key(index);
  },
  get length() {
    return localStorage.length;
  },
};
